package com.jerry.constellation.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.jerry.constellation.Adapter.StarBaseAdapter;
import com.jerry.constellation.Adapter.StarPagerAdapter;
import com.jerry.constellation.R;
import com.jerry.constellation.activity.StardetailsActivity;
import com.jerry.constellation.bean.StarInfoBean;

import java.util.ArrayList;
import java.util.List;

public class Star_Fragment extends Fragment {
    ViewPager starVp;
    GridView starGv;
    LinearLayout pointerLayout;
    private List<StarInfoBean.StarinfoBean> mdata;
    private StarBaseAdapter starBaseAdapter;
    //viewpage的图片数组
    int[] viewpage_pics = new int[]{
            R.mipmap.pic_guanggao, R.mipmap.pic_star, R.mipmap.pic_03
    };
    //声明Viewpage的数据源
    List<ImageView> ivList;
    //声明指示器小圆点
    List<ImageView> pointList;
    private StarPagerAdapter starPagerAdapter;
    //完成定时切换
    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            if (msg.what == 1) {
                //获取viewpage当前显示的页面
                int currentItem = starVp.getCurrentItem();
                //判断最后一张，返回第一张
                if (currentItem == ivList.size() - 1) {
                    starVp.setCurrentItem(0);
                } else {
                    currentItem++;
                    starVp.setCurrentItem(currentItem);
                }
                //接收的同时，发送消息==形成循环发送
                handler.sendEmptyMessageDelayed(1, 3000);

            }
        }
    };

    /*表示星座的frag */
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.star_fragment, container, false);
        initView(view);
        Bundle bundle = getArguments();
        StarInfoBean infoBean = (StarInfoBean) bundle.getSerializable("info");
        mdata = infoBean.getStarinfo();//获取关于星座信息的集合数据

        //创建适配器
        starBaseAdapter = new StarBaseAdapter(getContext(), mdata);
        starGv.setAdapter(starBaseAdapter);
        initPager();
        setVPListerner();
        //为Gridview的每一项添加事件监听
        setGvListerner();
        //发送延时消息,通知可以切换viewpage的图片
        handler.sendEmptyMessageDelayed(1, 3000);
        return view;
    }

    /* Gridview的每一项添加事件监听 */
    private void setGvListerner() {
        starGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                StarInfoBean.StarinfoBean starinfoBean = mdata.get(i);
                Intent intent = new Intent(getContext(), StardetailsActivity.class);
                intent.putExtra("star",starinfoBean);
                startActivity(intent);
            }
        });
    }


    private void initPager() {
        //设置viewPage显示
        ivList = new ArrayList<>();
        pointList = new ArrayList<>();
        for (int i = 0; i < viewpage_pics.length; i++) {
            ImageView imageView = new ImageView(getContext());
            imageView.setImageResource(viewpage_pics[i]);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            //设置图片view的宽高
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT);
            imageView.setLayoutParams(lp);
            //将图片加载到集合当中
            ivList.add(imageView);
            //创建图片随对应的指示器
            ImageView pointerImg = new ImageView(getContext());
            pointerImg.setImageResource(R.mipmap.point_normal);
            LinearLayout.LayoutParams layoutParams_pointer = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            layoutParams_pointer.setMargins(20, 0, 0, 0);
            pointerImg.setLayoutParams(layoutParams_pointer);
            //将小圆点添加到布局中
            pointerLayout.addView(pointerImg);
            //为了方便管理，将小圆点添加到统一管理
            pointList.add(pointerImg);
        }
        //默认第一个小圆点是获取焦点状态
        pointList.get(0).setImageResource(R.mipmap.point_focus);
        //创建适配器
        starPagerAdapter = new StarPagerAdapter(getContext(), ivList);
        starVp.setAdapter(starPagerAdapter);
    }

    /*
     * TODO:设置viewPager监听
     * */
    private void setVPListerner() {
        starVp.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < pointList.size(); i++) {
                    pointList.get(i).setImageResource(R.mipmap.point_normal);
                }
                pointList.get(position).setImageResource(R.mipmap.point_focus);
            }
        });
    }

    private void initView(View view) {
        starVp = view.findViewById(R.id.starfrg_vp);
        starGv = view.findViewById(R.id.starfrg_gv);
        pointerLayout = view.findViewById(R.id.starfrg_pointer);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }
}
